package cn.laixueit.listener;

import cn.laixueit.utils.SmsUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Component
public class SmsListener {

    @Autowired
    private SmsUtils smsUtils;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "chuangmei.sms.queue",durable = "true"),
            exchange = @Exchange(value = "chuangmei.sms.exchange",ignoreDeclarationExceptions = "true"),
            key = {"sms.code"}
    ))
    public void listenerSms(Map<String,String> msg){
        if(CollectionUtils.isEmpty(msg) || msg.size()==0){
            return;
        }
        String phone = msg.get("phone");
        String code = msg.get("code");
        if(phone==null || code == null){
            return;
        }
        try {
            smsUtils.sendSms(msg.get("phone"),msg.get("code"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
